package com.example.wiki3.activaty;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wiki3.R;


public class FirstActivity extends Activity implements View.OnClickListener {
    private Button register, login;
    private EditText username, password;

    private void find() {
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        username = findViewById(R.id.username_first);
        password = findViewById(R.id.password_first);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        //初始化获取的布局元素
        find();

        login.setOnClickListener(this);
        register.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.login) {
            String n = username.getText().toString();
            String p = password.getText().toString();
            if (n.isEmpty()) {
                Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            } else if (p.isEmpty()) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            } else {
                // 这里假设你有一个验证用户名和密码的方法，比如从服务器验证
                // 由于你原本的代码中没有实际的验证逻辑，这里简化处理
                if (isValidUser(n, p)) { // 假设 isValidUser 是验证用户的方法
//                    Toast.makeText(this, "登录成功！", Toast.LENGTH_SHORT).show();
                    saveLoginStatus(n, p); // 保存登录状态

                    // 准备并启动主界面，传递用户信息
                    Intent home = new Intent(FirstActivity.this, MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", n);
                    // 注意：下面的emil和phone应该从实际的数据库查询中获取，这里简化处理
                    bundle.putString("emil", "example@example.com");
                    bundle.putString("phone", "1234567890");
                    home.putExtras(bundle);
                    username.setText("");
                    password.setText("");
                    startActivity(home);
                    finish(); // 登录成功后关闭登录界面

                } else {
                    Toast.makeText(this, "账号或密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                    password.setText("");
                }
            }
        } else if (id == R.id.register) {
            Intent re = new Intent(FirstActivity.this, RegisterActivity.class);
            startActivity(re);
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    private Boolean saveLoginStatus(String username, String password) {

        SharedPreferences sharedPreferences = getSharedPreferences("login_prefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isLoggedIn", true);
        editor.putString("username", username);
        editor.putString("password", password);
        editor.apply();
        return  true;

    }

    private boolean isValidUser(String username, String password) {

        SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        String name1 = sharedPreferences.getString("username", "");
        String password1 = sharedPreferences.getString("password", "");

        return name1.equals(username) && password1.equals(password);

    }


}